package com.tcwlkj.uuService.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.tcwlkj.uuService.MyApp;
import com.tcwlkj.uuService.NetWork.HttpCallBack;
import com.tcwlkj.uuService.NetWork.RetrofitUtils;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.model.CommonResponse;
import com.tcwlkj.uuService.utils.ChcheUtils;
import com.tcwlkj.uuService.utils.EventCenter;
import com.tcwlkj.uuService.utils.PreferencesUtils;
import com.tcwlkj.uuService.utils.ToastUtil;
import com.tcwlkj.uuService.utils.Tools;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;

/**
 * Created by Administrator on 2017/10/25 0025.
 */

public class CashWithdrawalActivity extends BaseActivity {
    @Bind(R.id.tv_cardnum_drawal)
    TextView tv_cardnum_drawal;
    @Bind(R.id.edit_money_drawal)
    EditText edit_money_drawal;
    @Bind(R.id.btn_getmoney_drawal)
    Button btn_getmoney_drawal;
    private String money;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cash_money);
        ButterKnife.bind(this);
        initview();
    }
    private void initview() {
        changeTitle("提现");
        money = getIntent().getStringExtra("money");
        String str = ChcheUtils.getInstance().getDriverDetailModel().getCard().getNumber();
        tv_cardnum_drawal.setText(str.substring(str.length() - 4, str.length()));
    }
    @Override
    protected void onStart() {
        super.onStart();
        btn_getmoney_drawal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Tools.isFastClick()){
                    String amount = edit_money_drawal.getText().toString().trim();
                    if (TextUtils.isEmpty(amount)) {
                        ToastUtil.getInstance().show("提现金额不能为空");
                        return;
                    }
                    if (Double.parseDouble(amount)>Double.parseDouble(money)) {
                        ToastUtil.getInstance().show("提现金额不足");
                        return;
                    }
                    if(!(Double.parseDouble(amount)>=100&&Double.parseDouble(amount)%100==0)){
                        ToastUtil.getInstance().show("提现金额必须为100整倍数");
                        return;
                    }
                    Call<CommonResponse<Object>> httpResultCall = RetrofitUtils.getRequest(CashWithdrawalActivity.this, true).withdraw(PreferencesUtils.getString(MyApp.getContext(), "userID"),amount);
                    httpResultCall.enqueue(new HttpCallBack<CommonResponse<Object>>() {
                        @Override
                        public void onSuccess(Object t) {
                            CommonResponse<Object> commonResponse= (CommonResponse<Object>) t;
                            ToastUtil.getInstance().show(commonResponse.getMessage());
                            EventBus.getDefault().post(new EventCenter<String>(1000,"提现成功"));
                            finish();
                        }
                        @Override
                        public void onFail(int code, String message) {
                            if(code == 40104){
                                onStart();
                            }else {
                                ToastUtil.getInstance().show(message);
                            }
                        }
                    });
                }
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
